package com.atguigu.beans;

import java.io.Serializable;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

public class User implements Serializable,HttpSessionBindingListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Integer id;
	private String username;
	private String password;
	private String email;

	public User(Integer id, String username, String password, String email) {
		super();
		this.id = id;
		this.username = username;
		this.password = password;
		this.email = email;
	}

	public User() {
		super();
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password=" + password + ", email=" + email + "]";
	}

	//当向session域中添加User对象时会调用该方法
	@Override
	public void valueBound(HttpSessionBindingEvent event) {
		//获取Session对象
		HttpSession session = event.getSession();
		//获取ServletContext对象（application域）
		ServletContext application = session.getServletContext();
		//从application域中获取当前的在线人数
		Integer count = (Integer) application.getAttribute("count");
		if(count == null) {
			//证明你是第一个上线的，设置当前在线人数为1
			application.setAttribute("count", 1);
		}else {
			//证明已经有在线的人，此时只需要在之前数量的基础上加1即可
			application.setAttribute("count", count+1);
		}
	}

	//将User对象从session域中移出时会调用该方法
	@Override
	public void valueUnbound(HttpSessionBindingEvent event) {
		
	}

}
